package com.roi.njzk_big_api.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import com.roi.njzk_big_api.dto.RedisDTO;
import com.roi.njzk_big_api.entity.AirConditioning;
import com.roi.njzk_big_api.entity.BulbSwitch;
import com.roi.njzk_big_api.service.IAirConditioningService;
import com.roi.njzk_big_api.service.IBulbSwitchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Jason
 * @since 2020-09-22
 */
@RestController
@RequestMapping("/air-conditioning")
public class AirConditioningController {
    @Autowired
    private IAirConditioningService iAirConditioningService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/list")
    public List<AirConditioning> getList(){
        List<AirConditioning> list = iAirConditioningService.list();
        if(CollectionUtil.isNotEmpty(list)){
            for(AirConditioning airConditioning : list){
                Object object = stringRedisTemplate.opsForValue().get(airConditioning.getAddress() + ":" + airConditioning.getEndpointId());
                if (!BeanUtil.isEmpty(object)) {
                    RedisDTO redisDTO = JSONUtil.toBean(object.toString(), RedisDTO.class);
                    airConditioning.setState(Integer.parseInt(redisDTO.getState().getState()));
                }
            }
        }
        return list;
    }
}
